package com.example.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.Vo.employeeVO;
import com.example.entity.department;
import com.example.entity.employee;
import com.example.util.PageResult;
import com.example.service.IdepartmentService;
import com.example.service.IemployeeService;
import com.example.mapper.employeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class employeeServiceImpl extends ServiceImpl<employeeMapper, employee> implements IemployeeService {
    @Autowired
    private IdepartmentService departmentService;
    @Override
    public PageResult<employee> findByPage(Integer page, Integer pageSize) {
        Page<employee> Page = this.page(new Page<>(page, pageSize));
        return new PageResult<>(Page.getRecords(),Page.getTotal());
    }//分页处理
    @Override
    public PageResult<employee> search(Integer page, Integer pageSize, employeeVO vo) {
        QueryWrapper<employee> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getEmployeeName()), "employeeName", vo.getEmployeeName());
        wrapper.like(StrUtil.isNotBlank(vo.getAddr()), "addr", vo.getAddr());
        wrapper.eq(vo.getDepartmentId() != null && vo.getDepartmentId() != 0, "departmentId", vo.getDepartmentId());
        Page<employee> page1 = this.page(new Page<>(page, pageSize), wrapper);
        page1.getRecords().forEach(st -> {
            department department = departmentService.getById(st.getDepartmentId());
            st.setDepartmentName(department.getDepartmentName());
        });
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }//分页查询处理
    @Override
    public boolean add(employee employee) {
        return this.save(employee);
    }
}
